package org.finmanager.budget;

import org.finmanager.core.Category;
import org.finmanager.data.BudgetList;

import javax.swing.table.AbstractTableModel;

import java.util.GregorianCalendar;

import static org.finmanager.core.Constants.BUDGET_TABLE_COLUMN_NAME;

/**
 * Created by Kelvin on 4/4/2014.
 */

//enhancing the JTABLE used in the planning function
public class BudgetTableModel extends AbstractTableModel {
	private BudgetList budgetList;

	public static final int CATEGORY = 0;
	public static final int AMOUNT = 1;
	public static final int REMAINING = 2;
	public static final int RATIO = 3;
	public static final int DATE = 4;

	public BudgetTableModel(BudgetList budgetList) {
		this.budgetList = budgetList;
	}

	@Override
	public int getRowCount() {
		return budgetList.getBudgetCount();
	}

	@Override
	public int getColumnCount() {
		return BUDGET_TABLE_COLUMN_NAME.length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Budget budget = budgetList.getBudget(rowIndex);
		switch (columnIndex) {
			case CATEGORY   :return budget.getCategory();
			case AMOUNT     :return budget.getAmount();
			case REMAINING  :return budget.getRemaining();
			case RATIO      :return (int)Math.round(budget.getRemaining()/budget.getAmount() * 100);
			case DATE       :return budget.getDate();
			default         :return null;
		}
	}

	@Override
	public String getColumnName(int column) {
		return BUDGET_TABLE_COLUMN_NAME[column];
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		switch (columnIndex) {
			case CATEGORY   :return Category.class;
			case AMOUNT     :return Double.class;
			case REMAINING  :return Double.class;
			case RATIO      :return Integer.class;
			case DATE       :return GregorianCalendar.class;
			default         :return null;
		}
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}
}
